import { SearchField, Flex, View } from '@aws-amplify/ui-react'; import { Example, ExampleCode } from '@/components/Example'; import { Fragment } from '@/components/Fragment'; import { ComponentClassTable } from '@/components/ComponentClassTable'; import StandardHTMLAttributes from '@/components/StandardHTMLAttributes.mdx'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import ThemeExample from '@/components/ThemeExample.mdx'; import { SearchFieldDemo } from './demo'; import { DefaultSearchFieldExample, PlaceholderSearchFieldExample, RefExample, SearchFieldControlledExample, SearchFieldStyledPropsExample, SearchFieldThemeExample, SizeSearchFieldExample, VariationSearchFieldExample, } from './examples'; Users may clear the field by hitting the `Esc` key or by clicking the clear button. When users hit `Enter` key or click the search icon, the `onSubmit` event handler will be fired. ## Demo ## Usage Import the `SearchField` primitive, and provide a `label` for accessibility/usability. ```jsx file=./examples/DefaultSearchFieldExample.tsx ``` **Note**: The clear text (x) button will show after user has entered text. ### Controlled component ```tsx file=./examples/SearchFieldControlledExample.tsx ``` ***Note***: When you use SearchField in controlled way, it is your responsibility to set up `onClear` other than `onChange` since the input value is under your control. ### Accessibility / Label behavior {() => import('./../shared/formFieldAccessibility.mdx')} ### Placeholder Text that appears in the form control when it has no value set. ```jsx file=./examples/PlaceholderSearchFieldExample.tsx ``` ### Sizes Use the `size` prop to change `SearchField` size. Available options are `small`, `large`, and none (default). ```jsx file=./examples/SizeSearchFieldExample.tsx ``` ### Variations There are two variation styles available: default and `quiet`. ```jsx file=./examples/VariationSearchFieldExample.tsx ``` ### Forward refs {() => import('./../shared/forwardRefAlert.mdx')} The standard `ref` prop will forward to the `input` element, the `searchButtonRef` prop forwards to the search `button` element. The following is a contrived example demonstrating use of the ref props: ```tsx file=./examples/refs.tsx ``` ```jsx ``` ## CSS Styling ```tsx file=./examples/SearchFieldThemeExample.tsx ``` ### Target classes ### Global styling To override styling on all SearchField primitives, you can set the Amplify CSS variables or use the built-in `.amplify-searchfield` class. ```css /* styles.css */ :root { --amplify-components-button-color: black; --amplify-components-button-border-color: black; --amplify-components-fieldcontrol-border-color: black; } /* OR */ .amplify-searchfield { --amplify-components-button-color: black; --amplify-components-button-border-color: black; --amplify-components-fieldcontrol-border-color: black; } ``` ```css /* styles.css */ .amplify-searchfield { --amplify-components-button-color: rebeccapurple; --amplify-components-button-border-color: rebeccapurple; --amplify-components-fieldcontrol-border-color: rebeccapurple; } ``` ### Local styling To override styling on a specific SearchField, you can use a class selector or style props. _Using a class selector:_ ```css /* styles.css */ .custom-searchfield-class .amplify-input, .custom-searchfield-class .amplify-button { border-radius: 0; } ``` _Using style props:_ All style props will be applied to the [`Flex`](flex) wrapper of the `SearchField`. To style the `input` of the `SearchField`, you can pass a `inputStyles` prop with the style props you want to apply to the input. ```jsx file=./examples/SearchFieldStyledPropsExample.tsx ```